/*
 * Copyright (c) 2017,重庆阿莫比科技有限公司,All Rights Reserved.
 */
package com.wowocai.btchina.entity;

import java.util.ArrayList;
import java.util.List;

/**
 * <br/>
 * Date: 2017年3月17日 下午6:07:14 <br/>
 * 
 * @Author 刘建成
 * @Version
 */
public class PagerEntity<T> {

    private final static int DEFAULT_PAGE_SIZE = 10;

    private long start = 1;

    private long end = 1;

    private boolean hasMore = false;

    private boolean hasPre = false;

    /**
     * 
     * Creates a new instance of PagerEntity.
     * 
     * @param total
     */
    public PagerEntity(long total) {
        this(total, DEFAULT_PAGE_SIZE);
    }

    private void initPager() {
        if (pageNo % pageSize > 0) {
            start = (pageNo / pageSize * pageSize) + 1;
            end = (pageNo / pageSize + 1) * pageSize;
        } else {
            start = (pageNo / pageSize - 1) * pageSize + 1;
            end = pageNo;
        }
        if (end > totalPage) {
            end = totalPage;
        }
        hasPre = pageNo > pageSize;
        hasMore = end < totalPage;
    }

    /**
     * Creates a new instance of PagerEntity.
     */
    public PagerEntity(long total, int pageSize) {
        this.total = total;
        this.setPageSize(pageSize);

        if (total == 0 || total <= pageSize) {
            totalPage = 1;
        } else {
            totalPage = total / pageSize;
            totalPage = total % pageSize > 0 ? totalPage + 1 : totalPage;
        }
        result = new ArrayList<T>(pageSize);
    }

    /**
     * 总条数
     */
    private long total = 0;

    /**
     * 当前页
     */
    private long pageNo = 1;

    /**
     * 每页显示条数
     */
    private long pageSize = 10;

    /**
     * 总页数
     */
    private long totalPage = 1;

    /**
     * 结果集
     */
    private List<T> result;

    /**
     * total.
     * 
     * @return the total
     */
    public long getTotal() {
        return total;
    }

    /**
     * total.
     * 
     * @param total the total to set
     */

    public void setTotal(long total) {
        this.total = total;
    }

    /**
     * totalPage.
     * 
     * @return the totalPage
     */

    public long getTotalPage() {

        return totalPage;

    }

    /**
     * totalPage.
     * 
     * @param totalPage the totalPage to set
     */

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * result.
     * 
     * @return the result
     */

    public List<T> getResult() {
        return result;
    }

    /**
     * result.
     * 
     * @param result the result to set
     */

    public void setResult(List<T> result) {
        this.result = result;
    }

    /**
     * pageNo.
     * 
     * @return the pageNo
     */

    public long getPageNo() {
        return pageNo;
    }

    /**
     * pageNo.
     * 
     * @param pageNo the pageNo to set
     */

    public void setPageNo(long pageNo) {
        this.pageNo = pageNo;
        initPager();
    }

    /**
     * pageSize.
     * 
     * @return the pageSize
     */

    public long getPageSize() {
        return pageSize;
    }

    /**
     * pageSize.
     * 
     * @param pageSize the pageSize to set
     */

    public void setPageSize(long pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * start.
     * 
     * @return the start
     */

    public long getStart() {
        return start;
    }

    /**
     * start.
     * 
     * @param start the start to set
     */

    public void setStart(long start) {
        this.start = start;
    }

    /**
     * end.
     * 
     * @return the end
     */

    public long getEnd() {
        return end;
    }

    /**
     * end.
     * 
     * @param end the end to set
     */

    public void setEnd(int end) {
        this.end = end;
    }

    /**
     * hasMore.
     * 
     * @return the hasMore
     */

    public boolean isHasMore() {
        return hasMore;
    }

    /**
     * hasMore.
     * 
     * @param hasMore the hasMore to set
     */

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    /**
     * hasPre.
     * 
     * @return the hasPre
     */

    public boolean isHasPre() {
        return hasPre;
    }

    /**
     * hasPre.
     * 
     * @param hasPre the hasPre to set
     */

    public void setHasPre(boolean hasPre) {
        this.hasPre = hasPre;
    }
}
